#include <iostream>
#include <vector>

using namespace std;

int res = 0;
int n = 0; // 2
vector<vector<int>> matrix(3, vector<int>(3));
vector<vector<bool>> barrier(10, vector<bool>(10));
int dx[5] = { 0, -1, 1, 0, 0 }; // static up down left right
int dy[5] = { 0, 0, 0, -1, 1 };

void dfs(int x, int y, int num)
{
    num = num * 10 + matrix[x][y]; // 111
    if (n == 0) {
        res = max(res, num);
        cout << num << endl;
        return;
    }
    for (int i = 0; i < 5; ++i)
    {
        int aimX = x + dx[i], aimY = y + dy[i]; // 0 0
        if (aimX >= 0 && aimX < 3 &&
            aimY >= 0 && aimY < 3 &&
            !barrier[matrix[x][y]][matrix[aimX][aimY]])
        {
            --n; // 0
            dfs(aimX, aimY, num); // 11
            ++n; // 1
        }
    }
}

int main()
{
    int cnt = 1;
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j) {
            matrix[i][j] = cnt++;
            // cout << matrix[i][j] << endl;
        }
    }
    int M = 0; cin >> M;
    while (M--)
    {
        int beg = 0, end = 0;
        cin >> beg >> end;
        barrier[beg][end] = true;
    }
    cin >> n; // 2
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j) {
            dfs(i, j, 0); // 0, 0, 0
        }
    }
    cout << "res: " << res << endl;
    return 0;
}